package org.wzg.aityweb.Config;

import dev.langchain4j.community.store.embedding.redis.RedisEmbeddingStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.ClassPathDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.spring.AiService;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.wzg.aityweb.Repostry.RedisMemory;

import java.util.List;

@Configuration
public class CommenConfig {

    @Autowired
    private OpenAiChatModel model;
    @Autowired
    private RedisMemory redisMemory;

    @Autowired
    private RedisEmbeddingStore redisEmbedding;
/*    @Bean      不可对每个用户实现隔离，导致共享会话ID，方案为提供会话消息ID隔离 如提供
     Chatmemory提供者
    public ChatMemory chatMemory(){
        MessageWindowChatMemory memory = MessageWindowChatMemory.
                builder()
                .maxMessages(20).build();
        return memory;
    }*/
    @Bean
    public ChatMemoryProvider chatMemoryProvider(){
            return new ChatMemoryProvider() {
                @Override
                public ChatMemory get(Object memoryId) {
                    MessageWindowChatMemory memory = MessageWindowChatMemory.
                            builder().id(memoryId)
                            .maxMessages(20).
                            chatMemoryStore(redisMemory).
                            build();
                    return memory;
                }
            };
    }
    //TODO：更改向量数据库操作对象从内存数据库切换为向量数据库(redis-search)
    @Bean
    public EmbeddingStore store(){
        //加载数据文件
        List<Document> documents = ClassPathDocumentLoader.loadDocuments("Context");
        //获取操作向量数据库操作对象
        //这里先采用内存
        /*InMemoryEmbeddingStore store = new InMemoryEmbeddingStore();*/
        //创建内存向量数据库
        //使用redis-search
        EmbeddingStoreIngestor build = EmbeddingStoreIngestor.builder().
                embeddingStore(redisEmbedding).build();
        //批量导入
        build.ingest(documents);
        //返回操作对象
        return redisEmbedding; //该返回值无意义，可看作void
    }
    //创建查询向量数据库对象
    @Bean
    public ContentRetriever contentRetriever(){
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(redisEmbedding).
                //搜索向量最小分数
                minScore(0.5).
                //搜索向量最大结果数
                maxResults(3)
                .build();

    }


}


